<?php
namespace Admin\Controller;
use Think\Controller;
class AcsuzhouController extends CommonController{

    
    /**
     * 奖品列表
     */
    public function prize_lists(){
        // exit("3");

      if(IS_GET){
        $DB = M('acsuzhou_prize');
        $list = $DB->order('grade_id')->select();

        $this->assign('list',$list);
        $this->display();
      }
    }




    /**
     * 中奖记录列表
     */
    public function prize_record_lists(){

      if(IS_GET){

      $pid = I('get.pid');
      if($pid == 0){
        $map['pid'] = 0;

      }else if($pid==1){
        $map['pid'] = array('gt',0);

      }
      $DB = M('acsuzhou_prize_record');

        /**
      数据分页显示开始
      **/
      $count      = $DB->where($map)->count();// 查询满足要求的总记录数
      $Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
      $show       = $Page->show();// 分页显示输出
      if($pid == 0){
        $sql = "select fd_acsuzhou_prize_record.*,fd_members.username as username,fd_acsuzhou_member_info.school as school,
        fd_acsuzhou_member_info.grade as grade,fd_acsuzhou_member_info.class as class,fd_acsuzhou_member_info.name as name,
        fd_acsuzhou_member_info.address as address,fd_acsuzhou_prize_repository.name as prize_name,fd_acsuzhou_prize_repository.img as img from 
        fd_acsuzhou_prize_record left join fd_members on fd_acsuzhou_prize_record.mid=fd_members.id left join fd_acsuzhou_member_info
         on fd_acsuzhou_prize_record.mid=fd_acsuzhou_member_info.mid left join fd_acsuzhou_prize_repository on fd_acsuzhou_prize_record.pid=
         fd_acsuzhou_prize_repository.id where pid=0"." limit ".$Page->firstRow.",".$Page->listRows;
      }else if($pid == 1){
        $sql = "select fd_acsuzhou_prize_record.*,fd_members.username as username,fd_acsuzhou_member_info.school as school,
        fd_acsuzhou_member_info.grade as grade,fd_acsuzhou_member_info.class as class,fd_acsuzhou_member_info.name as name,
        fd_acsuzhou_member_info.address as address,fd_acsuzhou_prize_repository.name as prize_name,fd_acsuzhou_prize_repository.img as img from 
        fd_acsuzhou_prize_record left join fd_members on fd_acsuzhou_prize_record.mid=fd_members.id left join fd_acsuzhou_member_info
         on fd_acsuzhou_prize_record.mid=fd_acsuzhou_member_info.mid left join fd_acsuzhou_prize_repository on fd_acsuzhou_prize_record.pid=
         fd_acsuzhou_prize_repository.id where pid>0"." limit ".$Page->firstRow.",".$Page->listRows;
      }
      // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
      $list = $DB->query($sql);
      /**
      数据分页结束
      **/
      // print_r($list); die;

        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->display();
      }
    }


    /**
     * 添加奖品
     */
    public function add_prize(){

      if(IS_GET){

        $this->display();
      }else{

        $data['name'] = trim(I('post.name'));
        $data['img'] = I('post.img');
        $data['grade_id'] = I('post.grade_id');
        $data['rate'] = trim(I('post.rate'));
        $ramark = trim(I('post.ramark'));
        if(!empty($ramark)){
            $data['ramark'] = $ramark;
        }
        $data['quantity'] = trim(I('post.quantity'));
        $data['ctime'] = I('server.REQUEST_TIME');
        $DB = M('acsuzhou_prize');
        $re = $DB->data($data)->add();
        if($re){
            
            /*
             * 添加用于抽奖的奖品记录
             */
            for($i=0;$i<$data['quantity'];$i++)
            {
                $dataList[]=array("parent_id"=>$re,"name"=>$data['name'],"img"=>$data['img'],"grade_id"=>$data['grade_id'],"ctime"=>I('server.REQUEST_TIME'));
            }
//            print_r($dataList);
            M("acsuzhou_prize_repository")->addAll($dataList);

          $this->success('添加成功',U('Admin/Acsuzhou/prize_lists'),1);
        }else{

          $this->error('添加失败!','',1);
        }
      }
    }


    /**
     * ajax 上传奖品图
     */
    public function upload_prize_img(){

      if(IS_AJAX){

        $up_location = 'acsuzhou/';
        $img_url = __ROOT__.'/Uploads/'.$this->upload($_FILES['fimg'],$up_location);

        if(!empty($img_url)){

          echo json_encode($img_url);
        }else{

          echo json_encode(0);
        }
      }
    }

    //  文件上传 图片格式上传
    private function upload($file,$up_location){

      $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize   =     31457280 ;// 设置附件上传大小
      $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
      $upload->savePath  =      $up_location; // 设置附件上传目录
      // 上传文件 
      $info   =   $upload->uploadOne($file);
      if(!$info){
      // 上传错误提示错误信息    
        $this->error($upload->getError());
      }else{   
        $file_url=$info['savepath'].$info['savename'];    
        return $file_url;
      }
    }


    /**
     * 删除奖品
     */
    public function del_prize(){
      if(IS_AJAX){

        $map['id'] = I('post.id');
        $DB = M('acsuzhou_prize');
        $re = $DB->where($map)->delete();

        if($re){
          echo json_encode(1);
        }else{
          echo json_encode(0);
        }
      }
    }

    /**
     * 删除中奖记录
     */
    public function del_prize_log(){
      if(IS_AJAX){

        $map['id'] = I('post.id');
        $DB = M('acsuzhou_prize_record');
        $re = $DB->where($map)->delete();

        if($re){
          echo json_encode(1);
        }else{
          echo json_encode(0);
        }
      }
    }


    /**
     * 修改奖品信息
     */
    public function modify_prize(){

        if(IS_GET){
            $map['id'] = I('get.id');
            $DB = M('acsuzhou_prize');

            $list = $DB->where($map)->find();

            $this->assign('list',$list);
            $this->display();
        }else{

          $data['name'] = trim(I('post.name'));
          $data['img'] = I('post.img');
          $data['rate'] = trim(I('post.rate'));
          $data['grade_id'] = I('post.grade_id');
          $ramark = trim(I('post.ramark'));
          if(!empty($ramark)){
              $data['ramark'] = $ramark;
          }
          $data['quantity'] = trim(I('post.quantity'));
          // $data['ctime'] = I('server.REQUEST_TIME');
          $map['id'] = I('post.id');

          $DB = M('acsuzhou_prize');
          $re = $DB->where($map)->data($data)->save();
          if($re){

            $this->success('修改成功!',U('Admin/Acsuzhou/prize_lists'),1);
          }else{

            $this->error('修改失败!','',1);
          }
        }
    }



    /**
     * 安全小卫士活动列表
     */
    public function xws_lists(){

        if(IS_GET){
            $DB = M('acsuzhou_xws_vote');

            /**
            数据分页显示开始
            **/
            $count      = $DB->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->lastSuffix=false;
                              $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                              $Page->setConfig('prev','上一页');
                              $Page->setConfig('next','下一页');
                              $Page->setConfig('last','末页');
                              $Page->setConfig('first','首页');
                              $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $sql = "select fd_acsuzhou_xws_vote.*,fd_members.username as username,fd_acsuzhou_member_info.name as name from 
            fd_acsuzhou_xws_vote left join fd_members on fd_acsuzhou_xws_vote.mid=fd_members.id left join 
            fd_acsuzhou_member_info on fd_acsuzhou_xws_vote.mid=fd_acsuzhou_member_info.mid order by fd_acsuzhou_xws_vote.vote_num
             desc"." limit "
            .$Page->firstRow.",".$Page->listRows;
            $list = $DB->query($sql);
            /**
            数据分页结束
            **/

            $PageCount = ($Page->firstRow)/40;
            foreach ($list as $key => $value) {
              # code...
              $list[$key]['rank'] = $PageCount * 40 + $key +1;
            }
            $this->assign('list',$list);
            $this->assign('show',$show);
            $this->display();
        }else{
            $DB = M('acsuzhou_xws_vote');
            $id = I('post.id');
            $mid = I('post.mid');

            if(!empty($id) && !empty($mid)){
                $sql = "select fd_acsuzhou_xws_vote.*,fd_members.username as username,fd_acsuzhou_member_info.name as name from fd_acsuzhou_xws_vote left join fd_members on fd_acsuzhou_xws_vote.mid=fd_members.id left join fd_acsuzhou_member_info on fd_acsuzhou_xws_vote.mid=fd_acsuzhou_member_info.mid where fd_acsuzhou_xws_vote.id=".$id." and fd_acsuzhou_xws_vote.mid=".$mid;
            }else if(!empty($id)){
                $sql = "select fd_acsuzhou_xws_vote.*,fd_members.username as username,fd_acsuzhou_member_info.name as name from fd_acsuzhou_xws_vote left join fd_members on fd_acsuzhou_xws_vote.mid=fd_members.id left join fd_acsuzhou_member_info on fd_acsuzhou_xws_vote.mid=fd_acsuzhou_member_info.mid where fd_acsuzhou_xws_vote.id=".$id;
            }else if(!empty($mid)){

                $sql = "select fd_acsuzhou_xws_vote.*,fd_members.username as username,fd_acsuzhou_member_info.name as name from fd_acsuzhou_xws_vote left join fd_members on fd_acsuzhou_xws_vote.mid=fd_members.id left join fd_acsuzhou_member_info on fd_acsuzhou_xws_vote.mid=fd_acsuzhou_member_info.mid where fd_acsuzhou_xws_vote.mid=".$mid;
            }

            $list = $DB->query($sql);
            // echo $DB->getLastSql(); die;
            // print_r($list); die;
            $this->assign('list',$list);
            // $this->assign('show',$show);
            $this->display();
        }
    }


    /**
     * 小卫士投票记录
     */
    public function xws_vote_log(){
      if(IS_GET){

          $wsid = I('get.id');
          $DB = M('acsuzhou_xws_vote_log');
          /**
          数据分页显示开始
          **/
          $count      = $DB->where(array('wsid' => $wsid))->count();// 查询满足要求的总记录数
          $Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
          $Page->lastSuffix=false;
                            $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                            $Page->setConfig('prev','上一页');
                            $Page->setConfig('next','下一页');
                            $Page->setConfig('last','末页');
                            $Page->setConfig('first','首页');
                            $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
          $show       = $Page->show();// 分页显示输出
          // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
          $sql = "select fd_acsuzhou_xws_vote_log.*,fd_members.username as username,fd_members.avatar as avatar from fd_acsuzhou_xws_vote_log left join 
          fd_members on fd_acsuzhou_xws_vote_log.openid=fd_members.wxopenid where wsid=".$wsid." limit ".$Page->firstRow.",".$Page->listRows;
          $list = $DB->query($sql);
          /**
          数据分页结束
          **/
          // echo $DB->getLastSql(); die;
          $this->assign('list',$list);
          $this->assign('show',$show);
          // print_r($list); die;
          $this->display();
      }
    }


    /**
     * 对数组进行排序
     */
    private function sortArrByField($array,$field){
        $fieldArr = array();
        foreach ($array as $k => $v) {
          $fieldArr[$k] = $v[$field];
        }

        array_multisort($fieldArr,SORT_DESC, $array);
        // print_r($array); die;
        return $array;
    }


    /**
     * 删除小卫士作品
     */
    public function del_xws_vote(){
      if(IS_AJAX){

        $map['id'] = I('post.id');
        $DB = M('acsuzhou_xws_vote');
        $re = $DB->where($map)->delete();

        if($re){
          echo json_encode(1);
        }else{
          echo json_encode(0);
        }
      }
    }



    /**
     * 题库列表
     */
    public function item_pool_lists(){
        if(IS_GET){

          $DB = M('acsuzhou_item_pool');

          $list = $DB->select();


          $this->assign('list',$list);
          $this->display();
        }
    }


    /**
     * 添加试题
     */
    public function add_item_pool(){

        if(IS_GET){



          $this->display();
        }else{

            $data['title'] = trim(I('post.title'));
            $data['optionA'] = trim(I('post.optionA'));
            $data['optionB'] = trim(I('post.optionB'));
            $data['optionC'] = trim(I('post.optionC'));
            $data['optionD'] = trim(I('post.optionD'));
            $data['right_option'] = trim(I('post.right_option'));
            $data['ctime'] = I('server.REQUEST_TIME');

            $DB = M('acsuzhou_item_pool');

            $re = $DB->data($data)->add();
            if($re){

              $this->success('添加成功!',U('Admin/Acsuzhou/item_pool_lists'),1);
            }else{

              $this->error('添加失败!','',1);
            }
        }
    }


    /**
     * 删除题目
     */
    public function del_item_pool(){

      if(IS_AJAX){
        $map['id'] = I('post.id');
        $DB = M('acsuzhou_item_pool');
        $re = $DB->where($map)->delete();

        if($re){
          echo json_encode(1);
        }else{
          echo json_encode(0);
        }
      }
    }


    /**
     * 修改题目
     */
    public function modify_item_pool(){

        if(IS_GET){
            $map['id'] = I('get.id');
            $DB = M('acsuzhou_item_pool');

            $list = $DB->where($map)->find();

            $this->assign('list',$list);
            $this->display();
        }else{

            $data['title'] = trim(I('post.title'));
            $data['optionA'] = trim(I('post.optionA'));
            $data['optionB'] = trim(I('post.optionB'));
            $data['optionC'] = trim(I('post.optionC'));
            $data['optionD'] = trim(I('post.optionD'));
            $data['right_option'] = trim(I('post.right_option'));
            $map['id'] = I('post.id');
            $DB = M('acsuzhou_item_pool');

            $re = $DB->where($map)->data($data)->save();
            if($re){

              $this->success('修改成功!',U('Admin/Acsuzhou/item_pool_lists'),1);
            }else{

              $this->error('修改失败!','',1);
            }
        }
    }



    /**
     * 投票检测列表
     */
    public function check_product_lists(){

        if(IS_GET){
            $ishidden = I('get.ishidden');

            if($ishidden == 1){
                $map['ishidden'] = 1;

            }else if($ishidden ==0 ){

                $map['type'] = 1;
                $map['ishidden'] = 0;

            }else if($ishidden == 2){

              $map['type'] = 0;
              $map['ishidden'] = 2;

            }else if($ishidden == 3){

              $map['type'] = 0;
              $map['ishidden'] = 0;
            }

            $DB = M('acsuzhou_product');

            /**
            数据分页显示开始
            **/
            $count      = $DB->where($map)->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->lastSuffix=false;
                              $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                              $Page->setConfig('prev','上一页');
                              $Page->setConfig('next','下一页');
                              $Page->setConfig('last','末页');
                              $Page->setConfig('first','首页');
                              $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            switch ($ishidden) {
              case 0:
                $sql = "select fd_acsuzhou_product.*,fd_members.username from fd_acsuzhou_product left join fd_members on 
                fd_acsuzhou_product.mid=fd_members.id"." where fd_acsuzhou_product.ishidden=0 and  fd_acsuzhou_product.type=1
                limit ".$Page->firstRow.",".$Page->listRows;
                # code...
                break;
              case 1:
                $sql = "select fd_acsuzhou_product.*,fd_members.username from fd_acsuzhou_product left join fd_members on 
                fd_acsuzhou_product.mid=fd_members.id"." where fd_acsuzhou_product.ishidden=1 
                limit ".$Page->firstRow.",".$Page->listRows;
                # code...
                break;
              case 2:
                $sql = "select fd_acsuzhou_product.*,fd_members.username from fd_acsuzhou_product left join fd_members on 
                fd_acsuzhou_product.mid=fd_members.id"." where fd_acsuzhou_product.ishidden=2 and fd_acsuzhou_product.type=0 
                limit ".$Page->firstRow.",".$Page->listRows;
                # code...
                break;
              default:
                $sql = "select fd_acsuzhou_product.*,fd_members.username from fd_acsuzhou_product left join fd_members on 
                fd_acsuzhou_product.mid=fd_members.id"." where fd_acsuzhou_product.ishidden=0 and fd_acsuzhou_product.type=0 
                limit ".$Page->firstRow.",".$Page->listRows;
                break;
            }
            $list = $DB->query($sql);
            /**
             * 获取投票数
             */
            $DB_vote_log = M('acsuzhou_vote_log');
            foreach ($list as $key => $value) {
              # code...
                $list[$key]['vote_num'] = $DB_vote_log->where(array('pid' => $value['id']))->count();
            }
            // echo $DB->getLastSql(); die;
            $this->assign('show',$show);
            $this->assign('list',$list);
            $this->display();
        }
    }


    /**
     * 删除检测商品
     */
    public function del_check_product(){

        if(IS_AJAX){

          $map['id'] = I('post.id');
          $DB = M('acsuzhou_product');
          $re = $DB->where($map)->delete();

          if($re){
            echo json_encode(1);
          }else{
            echo json_encode(0);
          }
      }
    }

    /**
     * 通过检测商品审核
     */
    public function agree_pass_check_product(){

        if(IS_AJAX){

          $map['id'] = I('post.id');
          $DB = M('acsuzhou_product');
          $re = $DB->where($map)->data(array('ishidden' => 0))->save();

          if($re){
            echo json_encode(1);
          }else{
            echo json_encode(0);
          }
      }  
    }
    /**
     * 
     * 修改你投票我检测产品图片
     */
    public function modify_product_img(){
        
        if(IS_GET){
          $id = I('get.id');

          $list = M('acsuzhou_product')->where(array('id' => $id))->find();

          $this->assign('list',$list);
          $this->display();
        }else{
          $map['id'] = I('post.id');
          $data['name'] = I('post.name');
//          if(!empty(I('post.product_img')))
//          {
//              $data['img'] = I('post.product_img');
//          }
        $product_img = I('post.product_img');

          if(!empty($product_img))
          {
              $data['img'] =$product_img;
          }
          

          $res = M('acsuzhou_product')->where($map)->data($data)->save();

          if($res){
            $this->success('修改成功!',U('Admin/Acsuzhou/check_product_lists',array('ishidden'=>1)),1);
          }else{

            $this->error('修改失败!');
          }
        }
    }


    /**
     * ajax修改产品图片
     */
    public function upload_product_img(){
      if(IS_AJAX){

        $up_location = 'acsuzhou/';
        $img_url = __ROOT__.'/Uploads/'.$this->upload($_FILES['fimg'],$up_location);

        if(!empty($img_url)){

          echo json_encode($img_url);
        }else{

          echo json_encode(0);
        }
      }
    }
    /**
     * 添加你投票我检测原始数据
     */
    public function add_product(){

      if(IS_GET){

        $this->display();
      }else{

        $data['name'] = trim(I('post.name'));
        $data['img'] = I('post.img');
        $data['type'] = 0;
        $data['ishidden'] = 2;
        $data['ctime'] = I('server.REQUEST_TIME');
        $data['mid'] = 0;

        $DB = M('acsuzhou_product');
        $res = $DB->data($data)->add();
        if(!empty($res)){

          $this->success('添加成功!',U('Admin/Acsuzhou/check_product_lists',array('ishidden' => 2)),1);
        }else{

          $this->error('添加失败!','',1);
        }
      }
    }

    /**
     * 你投票我检测投票详情
     */
    public function product_vote_log(){

      if(IS_GET){

        $id = I('get.id');
        $DB = M('acsuzhou_vote_log');

        $sql = "select fd_acsuzhou_vote_log.*,fd_members.username as username,fd_members.avatar as avatar from fd_acsuzhou_vote_log
         left join fd_members on fd_acsuzhou_vote_log.mid=fd_members.id where fd_acsuzhou_vote_log.pid=".$id;

        $list = $DB->query($sql);
        // echo $DB->getLastSql(); die;
        // print_r($list); die;
        $this->assign('list',$list);
        $this->display();
      }
    }



    /**
     * 考试成绩列表
     */
    public function examination_result(){

        if(IS_GET){

            $DB = M('acsuzhou_member_info');
            /**
            数据分页显示开始
            **/
            $count      = $DB->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->lastSuffix=false;
                              $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                              $Page->setConfig('prev','上一页');
                              $Page->setConfig('next','下一页');
                              $Page->setConfig('last','末页');
                              $Page->setConfig('first','首页');
                              $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $sql = "select fd_acsuzhou_member_info.*,fd_members.username from fd_acsuzhou_member_info left join fd_members on fd_acsuzhou_member_info.mid=fd_members.id"." limit ".$Page->firstRow.",".$Page->listRows;
            $list = $DB->query($sql);

            $this->assign('show',$show);
            $this->assign('list',$list);
            $this->display();
        }
    }
}
?>